package com.zpark.it.controller;

import com.zpark.it.param.CartListParam;
import com.zpark.it.param.CartSavaParam;
import com.zpark.it.pojo.Cart;
import com.zpark.it.service.CartService;
import com.zpark.it.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: CartController
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/26 11:19
 */
@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartServicel;

    @PostMapping("save")
    public R save(@RequestBody @Validated CartSavaParam cartSavaParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("参数为null,添加失败");
        }
        return cartServicel.save(cartSavaParam);
    }

    @PostMapping("list")
    public R list(@RequestBody @Validated CartListParam cartListParam,BindingResult result){
        if (result.hasErrors()){
            return R.fail("购物车数据查询失败！");
        }
        return cartServicel.list(cartListParam.getUserId());
    }

    @PostMapping("update")
    public R update(@RequestBody Cart cart){

        return cartServicel.update(cart);
    }

    @PostMapping("remove")
    public R remove(@RequestBody Cart cart){

        return cartServicel.remove(cart);
    }

    @PostMapping("remove/check")
    public R removeCheck(@RequestBody Integer productId){
        return cartServicel.check(productId);
    }
}
